﻿namespace dotnet_notes.Tools
{
    /// <summary>
    /// 业务扩展和工具类
    /// </summary>
    public static class BusinessTool
    {
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="updateable"></param>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public static async Task<bool> LogicalDeleteAsync<T>
            (this IUpdateable<T> updateable, int id) where T : BusinessBase, new()
        {
            return await updateable.Where(it => it.ID == id).SetColumns(it => new T
            {
                RecordStatus = RecordStatusEnum.Deleted,
                DeleteTime = DateTime.Now,
            })
            .ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 逻辑恢复
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="updateable"></param>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public static async Task<bool> LogicalRecoverAsync<T>
            (this IUpdateable<T> updateable, int id) where T : BusinessBase, new()
        {
            return await updateable.Where(it => it.ID == id).SetColumns(it => new T
            {
                RecordStatus = RecordStatusEnum.Active,
                RecoverTime = DateTime.Now,
            })
            .ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 检查并获取小写模块名(不存在则返回默认值)
        /// </summary>
        /// <param name="module">要检查的模块名</param>
        /// <returns></returns>
        public static string CheckModuleName(string? module)
        {
            string valid = module.GetValue().ToLower();

            return BusinessConfig.ModuleArray.Contains(valid) 
                ? valid
                : BusinessConfig.DefaultModule;
        }
    }
}
